package com.dxy.app.widget.universal_view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import com.dxy.app.R;
import com.dxy.app.utils.ScreenUtils;

/**
  *Created by duxingyu on 2017/7/25.15:30
  *Email:
  *Description:默认带有分割线的LinearLayout
  **/

public class DividerLinearLayout extends LinearLayout {
    public static String TAG;
    protected boolean isNeedDivider =true;
    public DividerLinearLayout(Context context) {
        super(context);
        setDivider();
    }

    public DividerLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        paraseAttr(context, attrs);
        setDivider();
    }

    public DividerLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paraseAttr(context,attrs);
        setDivider();
    }
    private void paraseAttr(Context context,AttributeSet attrs){
        TypedArray array =context.obtainStyledAttributes(attrs, R.styleable.DividerLinearLayout);
        if(array!=null){
            this.isNeedDivider =array.getBoolean(R.styleable.DividerLinearLayout_needDivider,isNeedDivider());
            array.recycle();
        }
    }


    private void setDivider(){
        TAG =getClass().getSimpleName();
        setBackgroundColor(getContext().getResources().getColor(getBackColor()));
        if(!isNeedDivider()){
            return;
        }
        setShowDividers(SHOW_DIVIDER_MIDDLE|SHOW_DIVIDER_END);
        setDividerPadding(getDividerMargin());
        setDividerDrawable(getDividerColor());
    }

    /**
     * 默认是需要分割线的
     * */
    protected boolean isNeedDivider(){
        return isNeedDivider;
    }
    /**
     * 获取drawable的样式
     * */
    protected Drawable getDividerColor(){
        GradientDrawable drawable =new GradientDrawable();
        drawable.setColor(getContext().getResources().getColor(R.color.divider_color));
        drawable.setSize(ScreenUtils.screenWidth,1);
        return drawable;
    }
    /**
     * 获取divider的margin值
     * */
    protected int getDividerMargin(){
        return 25;
    }

    protected int getBackColor(){
        return R.color.white;
    }
}
